package com.qf.smsplatfrom.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.smsplatfrom.cache.DirtyWordCache;
import com.qf.smsplatfrom.constans.ResultCode;
import com.qf.smsplatfrom.events.UpdateDirtyWordEvents;
import com.qf.smsplatfrom.exceptions.DirtyDataException;
import com.qf.smsplatfrom.exceptions.MyBaseException;
import com.qf.smsplatfrom.exceptions.UpdateDataException;
import com.qf.smsplatfrom.mapper.DirtyWordMapper;
import com.qf.smsplatfrom.pojo.CheckType;
import com.qf.smsplatfrom.pojo.DirtyWord;
import com.qf.smsplatfrom.pojo.MyAuthUser;
import com.qf.smsplatfrom.pojo.TAdminUser;
import com.qf.smsplatfrom.service.DirtyWordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class DirtyWordServiceImpl implements DirtyWordService {

    private DirtyWordMapper dirtyWordMapper;
    private ApplicationContext context;
    private DirtyWordCache dirtyWordCache;
    @Autowired
    public void setDirtyWordCache(DirtyWordCache dirtyWordCache) {
        this.dirtyWordCache = dirtyWordCache;
    }

    @Autowired
    public void setDirtyWordMapper(DirtyWordMapper dirtyWordMapper) {
        this.dirtyWordMapper = dirtyWordMapper;
    }
    @Autowired
    public void setContext(ApplicationContext context) {
        this.context = context;
    }

    @Override
    public DirtyWord showById(Long id) {
        if(ObjectUtils.isEmpty(id)||id<=0){
            throw new MyBaseException("传递的主键不合适", ResultCode.PK_NOT_ALLOW);
        }
        //return dirtyWordMapper.findDirtyWordById(id);
        return dirtyWordCache.getByKey(id);
    }

    @Override
    public void addDirtyWord(DirtyWord dirtyWord) {
        //判断传过来的敏感词是否是空的
        if(dirtyWord.isNull(CheckType.ADD)){
            throw new DirtyDataException("不能传过来一个空的敏感词",ResultCode.DIRTY_NUM_ERROR);
        }
       dirtyWordMapper.addDirty(dirtyWord);
       //发布一个新事件
       context.publishEvent(new UpdateDirtyWordEvents());
    }

    @Override
    @Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
    public PageInfo<DirtyWord> showAll(Integer pageNum,Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        //List<DirtyWord> dirtyWordList = dirtyWordMapper.showAll();
        //PageInfo<DirtyWord> pageInfo = new PageInfo<>(dirtyWordList);
        //获取所有的缓存中的对象
        List<DirtyWord> list = dirtyWordCache.getAllData();
        //获取当前登录的用户
        MyAuthUser principal = (MyAuthUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        //RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
        //Object user = ((ServletRequestAttributes) requestAttributes).getRequest().getSession().getAttribute("user");
        //过滤属于当前用户的所有条数
        List<DirtyWord> dirtyWords = list.stream().filter(dirtyWord -> {
            return dirtyWord.getOwntype()==1||dirtyWord.getCreater()==principal.getId()||principal.getType()==1;
        }).collect(Collectors.toList());
        //将过滤玩的所有信息放入一个新的集合中进行跳过截取操作
        List<DirtyWord> collect = dirtyWords.stream().skip((pageNum - 1) * pageSize).limit(pageSize).collect(Collectors.toList());
        //放入一个pageinfo中
        PageInfo<DirtyWord> pageInfo = new PageInfo<>(collect);
        //手动将pageinfo的总条数存入到pageinfo中
        pageInfo.setTotal(dirtyWords.size());
        return pageInfo;
    }

    @Override
    public void updateDirtyById(DirtyWord dirtyWord) {
        //判断传过来的格式是否正确
        if(dirtyWord.isNull(CheckType.UPDATE)){
            throw new UpdateDataException("传过来的格式不正确",ResultCode.DATA_ISNULL);
        }
        //调用方法查询他的用户权限
        DirtyWord wordById = dirtyWordMapper.findDirtyWordById(dirtyWord.getId());
        //判断他是不是管理员，以及判断他可以修改哪些数据
        if(dirtyWord.getOwntype()!=1&&dirtyWord.getCreater()!=wordById.getCreater()){
            throw new UpdateDataException("你无权修改这些数据",ResultCode.NO_PERMISSON);
        }
        //调用修改方法
        dirtyWordMapper.updateDirtyById(dirtyWord);
        context.publishEvent(new UpdateDirtyWordEvents());
    }

    @Override
    public void deleteDirtyById(List<Integer> ids) {
        //获取一个user对象
        MyAuthUser principal = (MyAuthUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        //RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
        //Object user = ((ServletRequestAttributes) requestAttributes).getRequest().getSession().getAttribute("user");
        //判断是否是管理员
        if(principal.getType()==1){
            //管理员的删除方法
            dirtyWordMapper.deleteDirtyByIds(ids);
        }else{
            //普通人的删除方法
            dirtyWordMapper.deleteNoDirtyByIds(ids,principal.getId());
        }
        //发送一个事件
        context.publishEvent(new UpdateDirtyWordEvents());
    }

}
